package com.example.aline.tracedrawdemo;

import android.app.Activity;
import android.graphics.Color;
import android.location.Location;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;

import java.util.ArrayList;
import java.util.List;

/**
 * LocationSource :定义了一个定位源，为地图提供定位数据。
 * OnLocationChangedListener:位置改变的监听接口。
 * AMapLocationClient:定位服务类。此类提供单次定位、持续定位、最后位置相关功能。
 * AMapLocationClientOption:定位参数设置，通过这个类可以对定位的相关参数进行设置.在AMapLocationClient进行定位时需要这些参数
 */
/*, AMap.OnMyLocationChangeListener*/
public class MainActivity extends AppCompatActivity
        implements  LocationSource,AMapLocationListener {
    private static final String TAG = "MainActivity";
    private MapView mMapView ;
    private AMap aMap;
    private Polyline mPolyline;
    private OnLocationChangedListener mListener;
    private AMapLocationClient mLocationClient;
    private AMapLocationClientOption mLocationOption;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.map);
        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState)，创建地图
        mMapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mMapView.getMap();
            initPoit();
        }
        initLine();
    }

    private void initLine() {
        List<LatLng> latLngs = new ArrayList<LatLng>();
        latLngs.add(new LatLng(39.999391, 116.135972));
        latLngs.add(new LatLng(39.898323, 116.057694));
        latLngs.add(new LatLng(39.900430, 116.265061));
        latLngs.add(new LatLng(39.955192, 116.140092));
        mPolyline = aMap.addPolyline(new PolylineOptions().
                addAll(latLngs).width(10)
                .color(Color.argb(122, 200, 20, 20))
                .setUseTexture(true));
    }

    private void initPoit() {
        aMap.setLocationSource(this);
        aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mMapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (mListener != null && aMapLocation != null) {
            Log.e(TAG, aMapLocation.getLatitude() + " " + aMapLocation.getLongitude());
            mListener.onLocationChanged(aMapLocation);// 显示系统小蓝点
            Toast.makeText(MainActivity.this, "456 "
                    + aMapLocation.getLatitude() + " "
                    + aMapLocation.getLongitude(), Toast.LENGTH_SHORT).show();
        }
    }

//    @Override
//    public void onMyLocationChange(Location location) {
//        // 定位回调监听
//        if (location != null) {
//            Toast.makeText(this, "onMyLocationChange 定位成功， lat: " + location.getLatitude() + " lon: " + location.getLongitude(), Toast.LENGTH_SHORT).show();
//            Log.e("amap", "onMyLocationChange 定位成功， lat: " + location.getLatitude() + " lon: " + location.getLongitude());
//            Bundle bundle = location.getExtras();
//            if (bundle != null) {
//                int errorCode = bundle.getInt(MyLocationStyle.ERROR_CODE);
//                String errorInfo = bundle.getString(MyLocationStyle.ERROR_INFO);
//                // 定位类型，可能为GPS WIFI等，具体可以参考官网的定位SDK介绍
//                int locationType = bundle.getInt(MyLocationStyle.LOCATION_TYPE);
//
//                /*
//                errorCode
//                errorInfo
//                locationType
//                */
//                Log.e("amap", "定位信息， code: " + errorCode + " errorInfo: " + errorInfo + " locationType: " + locationType);
//            } else {
//                Log.e("amap", "定位信息， bundle is null ");
//
//            }
//
//        } else {
//            Log.e("amap", "定位失败");
//        }
//    }

    //激活定位源
    @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        mListener = onLocationChangedListener;
        startlocation();
    }

    private void startlocation() {
        if (mLocationClient == null) {
            mLocationClient = new AMapLocationClient(this);
            mLocationOption = new AMapLocationClientOption();
            // 设置定位监听
            mLocationClient.setLocationListener(this);
            // 设置为高精度定位模式
            mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
            mLocationOption.setInterval(2000);
            // 设置定位参数
            mLocationClient.setLocationOption(mLocationOption);
            // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
            // 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用stopLocation()方法来取消定位请求
            // 在定位结束后，在合适的生命周期调用onDestroy()方法
            // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
            mLocationClient.startLocation();
        }
    }

    //停止定位
    @Override
    public void deactivate() {
        mListener = null;
        if (mLocationClient != null) {
            mLocationClient.stopLocation();
            mLocationClient.onDestroy();
        }
        mLocationClient = null;
    }
}
